//
//  GWStatuseTool.m
//  新浪微博
//
//  Created by qianfeng on 16/2/17.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWStatuseTool.h"
#import "GWHttpTool.h"
#import "MJExtension.h"
#import "FMDatabase.h"
@implementation GWStatuseTool
static FMDatabase *_fmDB;
+(void)initialize
{
    NSString *patn =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath =[patn stringByAppendingPathComponent:@"statuse.sqlite"];
    FMDatabase *fmDB =[FMDatabase databaseWithPath:docPath];
    if ([fmDB open]) {
       
        BOOL result =[fmDB executeUpdate:@"CREATE TABLE IF NOT EXISTS t_status (id integer PRIMARY KEY AUTOINCREMENT,access_token text NOT NULL,status_idstr text NOT NULL,status_dict blob NOT NULL);"];
        if (result) {
            NSLog(@"创表成功");
        }
        else
        {
            NSLog(@"创表失败");
        }
    }
    _fmDB =fmDB;
}
+(NSArray *)chcheStatuseArrayWithPareme:(GWHomeStatuseParameter *)pareme
{
    NSMutableArray *array =[NSMutableArray array];
    FMResultSet *resSet =nil;
    if(pareme.since_id)
    {
        resSet = [_fmDB executeQuery:@"SELECT * FROM t_status WHERE status_idstr> ? and access_token =? ORDER BY status_idstr DESC limit ?;",pareme.since_id,pareme.access_token,pareme.count];
    }
    else if (pareme.max_id)
    {
        resSet = [_fmDB executeQuery:@"SELECT * FROM t_status WHERE status_idstr<= ? and access_token =? ORDER BY status_idstr DESC limit ?;",pareme.max_id,pareme.access_token,pareme.count];
    }else
    {
        resSet = [_fmDB executeQuery:@"SELECT * FROM t_status WHERE access_token =? ORDER BY status_idstr DESC limit ?;",pareme.access_token,pareme.count];
    }
    
    while ([resSet next]) {
       NSData *data =  [resSet objectForColumnName:@"status_dict"];
        NSDictionary *dict =[NSKeyedUnarchiver unarchiveObjectWithData:data];
        GWStatuseModel *statuse =[GWStatuseModel objectWithKeyValues:dict];
        [array addObject:statuse];
    }
    return array;
}
+(void)saveStatuseWithStatuseArray:(NSArray *)statuseArray Access_token:(NSString *)access_token
{
    
    for (NSDictionary *status in statuseArray) {
        NSData *status_dict =[NSKeyedArchiver archivedDataWithRootObject:status];
        [_fmDB executeUpdate:@"INSERT INTO t_status (access_token,status_idstr,status_dict) VALUES (?, ?, ?);",access_token,status[@"idstr"],status_dict];
    }
    
}
+(void)homeStatuseWithParame:(GWHomeStatuseParameter *)pareme success:(void (^)(GWHomeStastuseResult *respondObj))success failure:(void (^)(NSError *error))failure
{
    
    NSArray *statuseArray =[self chcheStatuseArrayWithPareme:pareme];
    if (statuseArray.count>0) {
        if (success) {
            GWHomeStastuseResult *result =[[GWHomeStastuseResult alloc]init];
            result.statuses =statuseArray;
            success(result);
        }
    }
    else
    {
        [GWHttpTool get:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:pareme.keyValues success:^(id respondObj) {
            [self saveStatuseWithStatuseArray:respondObj[@"statuses"] Access_token:pareme.access_token];
            if (success) {
                GWHomeStastuseResult *result =[GWHomeStastuseResult objectWithKeyValues:respondObj];
                success(result);
            }
        } failure:^(NSError *error) {
            if (error) {
                failure(error);
            }
        }];
    }
    
}
+(void)sendStatuseWithParame:(GWSendStatuseParam *)pareme success:(void (^)(GWSendStatuseResult *resultt))success failure:(void (^)(NSError *error))failure
{
    [GWHttpTool post:@"https://api.weibo.com/2/statuses/update.json" parameters:pareme.keyValues success:^(id respondObj) {
        
        if (success) {
            GWSendStatuseResult *result =[GWSendStatuseResult objectWithKeyValues:respondObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (error) {
            failure(error);
        }
    }];
}
+(void)detailCommentWithParame:(GWCommentParam *)pareme success:(void (^)(GWCommentParamResult *))success failure:(void (^)(NSError *))failure
{
    [GWHttpTool get:@"https://api.weibo.com/2/comments/show.json" parameters:pareme.keyValues success:^(id respondObj) {
        
        if (success) {
            GWCommentParamResult *result =[GWCommentParamResult objectWithKeyValues:respondObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (error) {
            failure(error);
        }
    }];
}
@end
